#include "listsManager.h"

#include <sstream>
#include <fstream>
#include <iterator>
#include <vector>
#include <string>

using namespace std;

#define FILE_PATH "todoCpp.txt"
#define TODO_FILENAME ".todo"

string getTodoFilePath() {
    string todoFilePath = "";
    char* path = NULL;
    size_t size;
    path = getcwd(path,size);
    string currentPath = string(path);
    todoFilePath = currentPath + "/" + TODO_FILENAME;
    
    return todoFilePath;
}

int main(int argc, char* argv[]) {
    // create the todo
    Todolist todo("main");

    string todoFilePath = getTodoFilePath();
    ifstream todoFile(todoFilePath.c_str());

    // Check if there is already a saved todolist in the current path
    if(todoFile.good()) {
        todo.load(todoFilePath);
    }

    if(argc == 1) {
        if(todo.empty()) {
            cout << "There is nothing to do in this directory!" << endl;
        }
        else {
            todo.toString();
        }
    }
    else {
        string cmd(argv[1]);
        if(cmd == "rm"){
            todo.deleteTask(atoi(argv[2]));
            todo.toString();
            todo.save(todoFilePath);
        }
        else if(cmd == "add") {
            if(argc > 2) { 
                todo.addTask(argv[2]);
                todo.toString();
                todo.save(todoFilePath);
            }
        }
        else if(cmd == "help") {
            cout << "Todo 1.0 - The ultimate todolist for programmers" << endl;
        }
        else {
            cout << "This command does not exists. Type \"help\" for listing all the available commands" << endl;
        }

    }

}


/*
int main() {
    Todolist todo("main");
    todo.addTask("Manger du caca");
    ListsManager lm;
    lm.addList(&todo);
    todo.addTask("Faire du bateau");
    lm.toString();
    Todolist* todo2 = lm.getTodo(1);
    todo2->addTask("Faire de la byciclette");
    todo.toString();
}
*/
/*
int main() {
    // The todolist object
	Todolist todo("test");

    // If a todolist was saved we load it
    todo.load(FILE_PATH);

    // String for the user input
    string input = "";
    string task = "";

    // Boolean for going on in the while loop
    bool goOn = true;

    cout << "Welcome in TodoListCPP! (type \"help\" for instructions)" << "\n";
    while (goOn) {
        cout << "> ";
        getline(cin,input);

        istringstream ss(input);
        istream_iterator<string> begin(ss), end;

        //putting all the tokens in the vector
        vector<string> args(begin, end);

        if(args[0] == "list") {
                todo.toString();
        }
        else if( args[0] == "new") {
            cout << "What's your new task? : ";
            getline(cin,task);
            todo.addTask(task);
        }
        else if (args[0] == "del" && args.size() == 2) {
            if (args[1] == "all") {
                todo.deleteAllTasks();
            }
            else {
                todo.deleteTask(atoi(args[1].c_str()));
            }
        }
        else if (args[0] == "help") {
            cout << "This is a list of the basic commands of TodoCpp :" << endl;
            cout << "- \"list\" : lists all the current taks" << endl;
            cout << "- \"new\" : allow you to add a new task" << endl;
            cout << "- \"del + [position|all]\" : delete the task at the selected position or all the tasks" << endl;
            cout << "- \"quit\" : quit the program" << endl;
        }
        else if(args[0] == "quit") {
            goOn = false;
        }
        else if(args[0] == "save") {
            todo.save(FILE_PATH);
        }
        else {
            cout << "This command does not exists. Type \"help\" for listing all the available commands" << endl;
        }
    }
    return 0;
}
*/
